# coding: utf-8
from datetime import datetime

from sqlalchemy import Boolean, Column, DateTime, Integer, SmallInteger, String, UniqueConstraint, BigInteger
from sqlalchemy.ext.declarative import declarative_base

BaseOrmClass = declarative_base()


class Users(BaseOrmClass):
    __tablename__ = 'ceping_usermodel'

    id = Column(Integer, primary_key=True)
    password = Column(String(128), nullable=False)
    last_login = Column(DateTime())
    is_superuser = Column(Boolean(), nullable=False)
    first_name = Column(String(30), nullable=False)
    last_name = Column(String(30), nullable=False)
    email = Column(String(254), nullable=False)
    is_staff = Column(Boolean, nullable=False)
    is_active = Column(Boolean, nullable=False)
    date_joined = Column(DateTime, nullable=False)
    username = Column(String(150), unique=True)
    is_deleted = Column(Boolean, nullable=False)
    role = Column(Integer)
    phone = Column(String(255), unique=True)
    cmpid = Column(Integer, default=0)
    perid = Column(Integer, default=0)
    sex = Column(Integer, default=0)
    user_code = Column(String(10))
    user_code_send_time = Column(BigInteger, default=0)
    id_number = Column(String(20))  # 2019-4-23 企业用户这种情况下无权修改用户身份证号码
    token = Column(String(43))  # 加密秘钥
    wx_openid = Column(String(255))  # 2019-5-6
    pc_openid = Column(String(255))  # 2019-5-6
    unionid = Column(String(255), unique=True)  # 2019-5-6
    headimgurl = Column(String(255))  # 2019-5-6
    app = Column(String(32))

    union_id = Column(Integer(),comment="工会ID")
    edu = Column(Integer(),comment="教育水平")
    birth = Column(DateTime(),comment="出生日期")


    def exclude(self):
        return ('token', 'password', 'is_deleted', 'wx_openid', 'pc_openid', 'unionid')


class Persons(BaseOrmClass):
    __tablename__ = 'ceping_personusermodel'
    __table_args__ = (
        UniqueConstraint('user_id', 'company_id'),
    )

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    is_leader = Column(Boolean, nullable=False)
    sex = Column(Integer)
    company_id = Column(Integer(), index=True)
    framework_id = Column(Integer(), index=True)
    user_id = Column(Integer(), nullable=False)
    position = Column(String(255))
    address = Column(String(255))
    age = Column(SmallInteger)
    education = Column(String(255))
    job_number = Column(String(255))
    marital = Column(String(255))
    nation = Column(String(255))
    work_life = Column(Integer)
    work_place = Column(String(255))
    is_super_user = Column(Boolean, nullable=False)
    birthday = Column(String(255))
    major = Column(String(255))
    school = Column(String(255))
    is_edited = Column(Boolean, nullable=False)
    role = Column(Integer, default=0)
    is_deleted = Column(Boolean, default=False)


class UserLogin(BaseOrmClass):
    '''
    模型(表)备注:用户登陆信息
    '''
    __tablename__ = 'ceping_user_login'

    '''      '''
    uid = Column(Integer(), primary_key=True)

    """
    客户端类型:
    个人电脑端  0
    企业电脑端  1
    测评师电脑端 2
    管理员端 3
    个人微信端: 4   
    """
    client = Column(Integer(), primary_key=True)

    '''   登陆过期截止时间戳   '''
    expire = Column(BigInteger(), nullable=False)

    '''   登陆日期.   '''
    login_time = Column(DateTime(), default=datetime.now)

    '''   校验值   '''
    key = Column(String(50))

    def __repr__(self):
        return "ceping_user_login(expire='{self.expire}')".format(self=self)

    def exclude(self): return ()


class Applications(BaseOrmClass):
    '''
    模型(表)备注:用户登陆信息
    '''
    __tablename__ = 'applications'

    app_id = Column(String(32), primary_key=True)

    app_key = Column(String(43))

    allow_ips = Column(String(255))


class UserSmsCode(BaseOrmClass):
    """
    用户短信验证码
    单独分离出来是考虑到注册的情况,不要直接去user表搅合,验证通过了在添加到user表中去
    2020-01-06
    """
    __tablename__ = 'ceping_user_sms_code'

    """电话号码"""
    phone = Column(BigInteger, primary_key=True)
    """短信验证码"""
    msg_code = Column(Integer)
    """短信验证码发送的时间"""
    msg_code_send_time = Column(BigInteger)
